package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 衷岚
 * @version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<Setmeal> findByCategoryId(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.findByCategoryId(categoryId);

        return setmealList;
    }

    @Override
    public List<DishItemVO> findDishInSetmeal(Long id) {
        //根据套餐id查询包含的菜品
        List<DishItemVO> dishItemVOList = setmealMapper.findDishInSetmeal(id);
        return dishItemVOList;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐信息
        setmealMapper.update(setmeal);

        //删除关联的菜品信息
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //新增关联的菜品信息
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish :setmealDishList) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.add(setmealDishList);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        //查询未起售菜品数量
        int countDishStopStatus = setmealDishMapper.countDishStopStatus(id);
        //套餐中有未起售菜品时，套餐启用失败
        if (countDishStopStatus > 0 && status==StatusConstant.ENABLE) {
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        setmealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //批量删除套餐
        //判断ids是否为空
        if(ids == null || ids.size() == 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_NO_PRARM_ERROR);
        }
        //套餐是起售状态不能删除
        for (Long id :ids) {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐
        setmealMapper.deleteBatch(ids);
        //删除套餐关联的菜品
        setmealDishMapper.deleteBatchBySetmealId(ids);
    }

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //新增套餐
        setmealMapper.add(setmeal);
        //新增套餐关联菜品数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList == null || setmealDishList.size() == 0) {
            throw new BaseException("必须加入菜品");
        }
        for (SetmealDish setmealDish :setmealDishList) {
            setmealDish.setSetmealId(setmeal.getId());
        }

        setmealDishMapper.add(setmealDishList);
    }

    @Override
    public SetmealVO findById(Long id) {
        //多表查询套餐和分类名称
        Setmeal setmeal = setmealMapper.findById(id);
        //查询套餐关联的菜品
        List<SetmealDish> setmealDishList = setmealDishMapper.findBySetmealId(id);
        //封装
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);

        List<SetmealVO> list = setmealMapper.pageQuery(setmeal);
        Page page =(Page) list;

        return new PageResult(page.getTotal(),page.getResult());
    }
}
